/* 设置公共默认路径 */
axios.defaults.baseURL='http://localhost:8080/'
/* 设置别名 */
Vue.prototype.$http = axios
/* 屏蔽vue警告 */
Vue.config.productionTip = false
// 前置拦截器
axios.interceptors.request.use(config => {
    let token = localStorage.getItem("token");
    if(token){
        config.headers["token"] = token; // 让请求头上面携带token
    }
    return config;
},error => {
    Promise.reject(error)
})
//  静态资源拦截器
let url = location.href;
console.log(url);
// 如果访问的时受限资源,
if (url.indexOf("index.html") == -1 // 如果访问的俩不是index.html
    && url.indexOf("login.html") == -1 // 不是login.html
    && url.indexOf("register.html") == -1 // 不是register.html
    && url.indexOf("callback.html") == -1
    && url.indexOf("binder.html") == -1
    && url != "http://127.0.0.1/"
    && url != "http://bugtracker.itsource.cn/"
){
    // 没有登录就拦截
    if(localStorage.getItem("logininfo") == null){

        location.href = "login.html";
        localStorage.removeItem("token");
    }
}

// 后置拦截器 {success:false,message:"noLogin"}
axios.interceptors.response.use(config=>{
    let data = config.data;
    if(!data.success && "登录失败, 请重新登录"===data.message) {
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        location.href = "login.html";
    }
    return config;
},error => {
    Promise.reject(error)
})

/**
 * 解析url获取参数对象
 * @param url
 * @returns {{}}
 */
function parseUrlParams2Obj(url){
    //获取请求参数
    let paramStr = url.substring(url.indexOf("?")+1);//code=021FdXFa14mYlA016OGa1L3R4X0FdXFO&state=1
    let paramStrs = paramStr.split("&");//[code=021FdXFa14mYlA016OGa1L3R4X0FdXFO,state=1]
    let paramObj = {};
    for(let i=0;i<paramStrs.length;i++){
        let paramName = paramStrs[i].split("=")[0];//code
        let paramValue = paramStrs[i].split("=")[1];//021FdXFa14mYlA016OGa1L3R4X0FdXFO
        paramObj[paramName] = paramValue;
    }
    return paramObj;
}